package com.only4play.portal.web.config;

import com.only4play.portal.common.api.PortalConstants;
import com.only4play.portal.cp.api.CpService;
import com.only4play.portal.cp.api.impl.CpServiceImpl;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonMessage;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonOutMessage;
import com.only4play.portal.cp.config.impl.CpDefaultConfigImpl;
import com.only4play.portal.cp.message.CpMessageRouter;
import com.only4play.portal.cp.message.jsonmessage.JsonCpMessageRouter;
import com.only4play.portal.web.handler.JsonMessageLogHandler;
import com.only4play.portal.web.handler.LocationHandler;
import com.only4play.portal.web.handler.SubscribeHandler;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file PortalCpConfiguration
 * @brief PortalCpConfiguration
 * @details PortalCpConfiguration
 * @date 2024-06-07
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-06-07               liyuncong          Created
 */

@Getter
@Configuration
@EnableConfigurationProperties(PortalCpProperties.class)
public class PortalJsonCpConfiguration {

    private PortalCpProperties properties;

    @Getter
    private static Map<Integer, CpMessageRouter<CpJsonOutMessage, CpJsonMessage>> routers = new HashMap<>();
    private static Map<Integer, CpService> cpServices = new HashMap<>();

    @Autowired
    private JsonMessageLogHandler jsonMessageLogHandler;
    @Autowired
    private SubscribeHandler subscribeHandler;
    @Autowired
    private LocationHandler locationHandler;


    @PostConstruct
    protected void initialization() {
        // 模拟读取数据/接口调用查询出数据
        final String corpId = "98cvxs330cryn2ym";
        final String corpSecret = "wESwlOcmraDV2vx46rU3FOS4c2ry8FWh";
        final Integer agentId = 29527;
        final String agentToken = "ESDdjHDc.fzhDLCbPGhlErjMoYAx1aMvomuoU29ykkXEG53S3aQVgGyn7HkAPJ6gmX6HxytI7.RR02bDGA2OpTKT8d";
        final String agentAseKey = "VlusQ73bCci0e2WnT1lq88b0MdxmWpRx";
        final String apiUrl = "https://www.baidu.com";

        CpDefaultConfigImpl config = new CpDefaultConfigImpl();
        config.setAgentId(agentId);
        config.setCorpId(corpId);
        config.setCorpSecret(corpSecret);
        config.setToken(agentToken);
        config.setAesKey(agentAseKey);
        config.setBaseApiUrl(apiUrl);

        CpServiceImpl service = new CpServiceImpl();
        service.setConfigStorage(config);
        routers.put(agentId, this.newRouter(service));

        cpServices.put(agentId, service);
    }

    public static CpService getCpService(Integer agentId) {
        return cpServices.get(agentId);
    }

    private CpMessageRouter<CpJsonOutMessage, CpJsonMessage> newRouter(CpService wxCpService) {
        final CpMessageRouter<CpJsonOutMessage, CpJsonMessage> newRouter = new JsonCpMessageRouter(wxCpService);

        // do somethings
        // 消息处理handler
        newRouter.rule().handler(jsonMessageLogHandler).next();

        // 订阅事件
        newRouter.rule().async(false)
            .msgType(PortalConstants.JsonMsgType.EVENT)
            .event(PortalConstants.EventType.SUBSCRIBE)
            .handler(subscribeHandler)
            .end();

        // 上报地理位置事件
        newRouter.rule().async(false)
            .msgType(PortalConstants.JsonMsgType.EVENT)
            .event(PortalConstants.EventType.LOCATION)
            .handler(locationHandler)
            .end();

        return newRouter;
    }
}
